package com.example.demo.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@ApiModel(value = "通用返回结果")
@Getter
@Setter
public class Result<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "返回标记：成功标记=200，失败标记=失败码")
    private int code;

    @ApiModelProperty(value = "返回信息")
    private String message;

    @ApiModelProperty(value = "数据")
    private T data;

    /**
     * 状态码枚举
     */
    public enum ResultCode {
        SUCCESS(200, "操作成功"),
        SERVER_ERROR(500, "服务器错误"),
        BAD_REQUEST(400, "请求错误"),
        UNAUTHORIZED(401, "暂未登录或token已经过期"),
        FORBIDDEN(403, "没有相关权限"),
        VALIDATE_FAILED(404, "参数检验失败"),
        REQUEST_TIMEOUT(408, "请求超时"),
        CIRCUIT_BREAKER(501, "服务熔断"),
        BUSINESS_ERROR(1000, "业务异常");

        private final int code;
        private final String message;

        ResultCode(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }
    }

    /**
     * 成功返回结果
     * @param data 获取的数据
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    /**
     * 成功返回结果
     * @param data 获取的数据
     * @param message 提示信息
     */
    public static <T> Result<T> success(T data, String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> Result<T> failed(String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SERVER_ERROR.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 失败返回结果
     * @param message 提示信息
     * @param data 获取的数据
     */
    public static <T> Result<T> failed(String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SERVER_ERROR.getCode());
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> Result<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED.getMessage());
    }

    /**
     * 参数验证失败返回结果
     * @param message 提示信息
     */
    public static <T> Result<T> validateFailed(String message) {
        return failed(message);
    }

    /**
     * 未登录返回结果
     */
    public static <T> Result<T> unauthorized() {
        return failed(ResultCode.UNAUTHORIZED.getMessage());
    }

    /**
     * 未授权返回结果
     */
    public static <T> Result<T> forbidden() {
        return failed(ResultCode.FORBIDDEN.getMessage());
    }

    /**
     * 服务器错误返回结果
     */
    public static <T> Result<T> serverError() {
        return failed(ResultCode.SERVER_ERROR.getMessage());
    }

    /**
     * 服务器错误返回结果
     * @param message 提示信息
     */
    public static <T> Result<T> serverError(String message) {
        return failed(message);
    }

    /**
     * 服务熔断返回结果
     */
    public static <T> Result<T> circuitBreaker() {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.CIRCUIT_BREAKER.getCode());
        result.setMessage(ResultCode.CIRCUIT_BREAKER.getMessage());
        return result;
    }

    /**
     * 服务熔断返回结果
     * @param message 提示信息
     */
    public static <T> Result<T> circuitBreaker(String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.CIRCUIT_BREAKER.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 业务异常返回结果
     */
    public static <T> Result<T> businessError() {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.BUSINESS_ERROR.getCode());
        result.setMessage(ResultCode.BUSINESS_ERROR.getMessage());
        return result;
    }

    /**
     * 业务异常返回结果
     * @param message 提示信息
     */
    public static <T> Result<T> businessError(String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.BUSINESS_ERROR.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 业务异常返回结果
     * @param message 提示信息
     * @param data 错误数据
     */
    public static <T> Result<T> businessError(String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.BUSINESS_ERROR.getCode());
        result.setMessage(message);
        result.setData(data);
        return result;
    }
} 